<template>
  <div class="page-main">
    <CustomForm :formInline="form" @onSubmit="onSubmit" @resetTable="resetTable">
      <el-form-item label="姓名">
        <el-input v-model="form.name" placeholder="输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="form.tel" placeholder="输入电话"></el-input>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="pickerData"
          @change="pickerChange"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
    </CustomForm>
    <el-table
      :data="list"
      style="width: 100%"
      height="705"
      v-loading="tableloading"
      border
      ref="multipleTable"
    >
    <el-table-column
        prop="user_name"
        label="用户名"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="tel"
        label="电话"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="udid"
        label="设备编号"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="os"
        label="系统"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="version"
        label="版本"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="deviceModel"
        label="型号"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="ip"
        label="IP"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="add_time"
        label="最后登录时间"
        align="center"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="deleteItem(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="form.page"
      :page-sizes="$pageSizeList"
      :page-size="form.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { deviceLog,delDeviceLog } from "@/api/set";
export default {
  name: "index",
  data() {
    return {
      form: {
        name: "",
        tel: "",
        time: "", //2024-05-06 / 2024-05-07
        page: 1,
        pageSize: 10
      },
      list: [],
      total: 0,
      pickerData: [],
      tableloading: false
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    async initial() {
      this.tableloading = true;
      let result = await deviceLog(this.form);
      this.tableloading = false;
      if (result.code == 200) {
        this.list = result.data.data ? result.data.data : [];
        this.total = result.data.total ? result.data.total : 0;
      }
    },
    onSubmit() {
      this.initial();
    },
    resetTable() {
      this.form = {
        name: "",
        time: "", //2024-05-06 / 2024-05-07
        page: 1,
        pageSize: 10
      };
      this.pickerData = [];
      this.initial();
    },
    pickerChange(val) {
      if (val && val.length > 0) {
        this.form.time = val.join("/");
      } else {
        this.form.time = [];
      }
    },
    handleSizeChange(pageSize) {
      this.form.pageSize = pageSize;
      this.initial();
    },
    handleCurrentChange(page) {
      this.form.page = page;
      this.initial();
    },
    deleteItem(row){
      this.$confirm("确定要删除该设备吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let result = await delDeviceLog({
            id: row.id
          });
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.resetTable();
          }
        })
        .catch(() => {

        });
    }
  }
};
</script>

<style lang="scss" scoped>
.deviceRecord {
  background-color: #fff;
  padding: 10px;
}
</style>
